package com.dudu.controller;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.dudu.bean.AccountBean;
import com.dudu.bean.AppExtendBean;
import com.dudu.service.IAccountExtendService;
import com.dudu.service.IAccountManageService;
import com.dudu.service.IAppExtendService;
import com.dudu.service.ICheckValidityService;
import com.dudu.service.ILogService;
import com.dudu.service.IOperationPlatformCustAccountService;
import com.dudu.service.ISmsCodeService;
import com.dudu.service.impl.AccountExtendServiceImpl;
import com.dudu.service.impl.AccountManageServiceImpl;
import com.dudu.service.impl.AppExtendServiceImpl;
import com.dudu.service.impl.CheckValidityServiceImpl;
import com.dudu.service.impl.LogService;
import com.dudu.service.impl.OperationPlatformCustAccountServiceImpl;
import com.dudu.service.impl.SmsCodeServiceImpl;
import com.dudu.utils.DateUtils;
import com.dudu.utils.EnumUtility;

import core.CoreService;

/**
 * 账号相关开放接口
 * 
 * @author gaoxs
 * @date 2016-4-19
 */
public class OpenAccountController extends CoreService implements IOpenAccountController {
    // 用于记录相应接口调用的日志
    private ILogService logService = new LogService();
    // app扩展信息操作
    private IAppExtendService appExtendService = new AppExtendServiceImpl();
    // 验证信息类
    private ICheckValidityService checkValidityService = new CheckValidityServiceImpl();
    // 账号管理相关
    private IAccountManageService accountManageService = new AccountManageServiceImpl();
    private IAccountExtendService accountExtendService = new AccountExtendServiceImpl();
    private IOperationPlatformCustAccountService custAccountService = new OperationPlatformCustAccountServiceImpl();
    // 短信
    private ISmsCodeService smsCodeServcie = new SmsCodeServiceImpl();

    /**
     * 赵希H5调用 账号信息列表接口(根据CUST_ACCOUNT) H5页面在注册前，H5页面输入“关联码”、关联手机号等然后调用此接口。
     * 返回与关联码一致的所有账号，让用户进行选择。
     * 
     * @return
     */
    public String qryMergeableOrgListByCust() {
	log.info("load qryMergeableOrgListByCust 账号信息列表接口");
	// 计算接口调用时间-开始
	long begin = Calendar.getInstance().getTimeInMillis();
	// 请求参数
	// app标识
	String app_key = this.getRequest().getParameter("app_key");
	String cust_account = this.getRequest().getParameter("cust_account");
	// 时间戳
	String timestamp = this.getRequest().getParameter("timestamp");
	// 认证码
	String sign = this.getRequest().getParameter("sign");

	// 用于应用自己调用记录信息-仅用于获取~直接返回
	String callback = this.getRequest().getParameter("callback");
	// 账号关联码
	String relevance_code = this.getRequest().getParameter("ext_orgReleCode");
	if (StringUtils.isBlank(relevance_code)) {
	    relevance_code = "";
	}
	// 关联手机号码
	String relevance_phone = this.getRequest().getParameter("ext_orgPhone");
	if (StringUtils.isBlank(relevance_phone)) {
	    relevance_phone = "";
	}
	// 验证码
	String auth_code = this.getRequest().getParameter("verifyCode");
	if (StringUtils.isBlank(auth_code)) {
	    auth_code = "";
	}
	// 用于后面获取企业详细信息
	JSONObject object = new JSONObject();
	try {
	    // 校验传入参数是否为空
	    if (StringUtils.isBlank(app_key) || StringUtils.isBlank(relevance_code) || StringUtils.isBlank(relevance_phone)) {
		log.info("qryMergeableOrgListByCust doAuth params:" + app_key + "，" + relevance_code + "，" + relevance_phone);
		object.put("result", -1);
		object.put("describe", "接口参数不完整");
		object.put("code", "0014");
	    } else {
		// 查询应用信息
		AppExtendBean appExtendBean = appExtendService.queryAppExtendByIdentify(app_key);
		// 判断app信息是否为空
		if (appExtendBean != null) {
		    // 根据账号cust_account查询账号信息
		    AccountBean accountBean = accountExtendService.queryAccountDetailByCustAccount(cust_account);
		    // 根据查询的账号信息
		    // 判断账号信息是否找到
		    if (accountBean != null) {
			// 验证秘钥信息
			if (checkValidityService.toolsCheckSign(cust_account, accountBean.getKey_private(), timestamp, sign)) {
			    // 判断auth_code是否正确，然后调用运营平台注册合并接口
			    // 校验短信验证码
			    if (smsCodeServcie.checkCode(relevance_phone, auth_code)) {
				object = accountManageService.queryCustAccountByRelateInfo(appExtendBean.getPeriphery_code(), relevance_phone, relevance_code);
			    } else {
				// 短信验证码错误
				object.put("result", "-1");
				object.put("describe", "验证码不正确");
			    }
			    // 获取返回结果
			    String result = (String) object.get("result");
			    // 判断注册是否成功
			    if (result.equals("0")) {
				object.put("result", "0");
				object.put("describe", "账号信息列表接口查询成功");
				object.put("code", "0000");
			    }
			} else {
			    object.put("result", "-1");
			    object.put("describe", "参数sign错误");
			    object.put("code", "0001");
			}
		    } else {
			object.put("result", "-1");
			object.put("describe", "未找到相应账号信息！");
			object.put("code", "0100");
		    }
		} else {
		    object.put("result", "-1");
		    object.put("describe", "请使用正确的应用标识");
		    object.put("code", "0007");
		}
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    object.put("result", "-1");
	    object.put("describe", "错误:账号信息列表接口查询发生异常" + e);
	    object.put("code", "0100");
	    log.error("账号信息列表接口查询" + e);
	}
	// 接口调用结束时间
	double end = Calendar.getInstance().getTimeInMillis();
	// 时间差
	double during = Double.valueOf((end - begin)) / 1000;
	// 拼日志内容
	String request_msg = "app_key:" + app_key + "|timestamp:" + timestamp + "|sign:" + sign + "|ext_orgReleCode:" + relevance_code + "|ext_orgPhone:" + relevance_phone
		+ "|verifyCode:" + auth_code + "|cust_account" + cust_account;
	this.logService.logInterface(during, "qryMergeableOrgListByCust", request_msg, "数据太长,未存储", object.getString("result"), DateUtils.getCurrentTime(), this.getRequest()
		.getRemoteAddr(), null);

	String data = "";
	if (StringUtils.isNotBlank(callback)) {
	    data = callback + "(" + object.toString() + ")";
	} else {
	    data = object.toString();
	}
	return returnData(data);
    }

    /**
     * 赵希H5调用 7.1.1 账号信息列表接口 H5页面在注册前，H5页面输入“关联码”、关联手机号等然后调用此接口。
     * 返回与关联码一致的所有账号，让用户进行选择。
     * 
     * @return
     */
    public String qryMergeableOrgList() {
	log.info("load qryMergeableOrgList 账号信息列表接口");
	// 计算接口调用时间-开始
	long begin = Calendar.getInstance().getTimeInMillis();
	// 请求参数
	// app标识
	String app_key = this.getRequest().getParameter("app_key");
	// 时间戳
	String timestamp = this.getRequest().getParameter("timestamp");
	// 认证码
	String sign = this.getRequest().getParameter("sign");

	// 用于应用自己调用记录信息-仅用于获取~直接返回
	String callback = this.getRequest().getParameter("callback");

	// 账号关联码
	String relevance_code = this.getRequest().getParameter("ext_orgReleCode");
	if (StringUtils.isBlank(relevance_code)) {
	    relevance_code = "";
	}
	// 关联手机号码
	String relevance_phone = this.getRequest().getParameter("ext_orgPhone");
	if (StringUtils.isBlank(relevance_phone)) {
	    relevance_phone = "";
	}
	// 验证码
	String auth_code = this.getRequest().getParameter("verifyCode");
	if (StringUtils.isBlank(auth_code)) {
	    auth_code = "";
	}
	// 用于后面获取企业详细信息
	JSONObject object = new JSONObject();
	try {
	    // 校验传入参数是否为空
	    if (StringUtils.isBlank(app_key) || StringUtils.isBlank(relevance_code) || StringUtils.isBlank(relevance_phone)) {
		log.info("qryMergeableOrgList doAuth params:" + app_key + "，" + relevance_code + "，" + relevance_phone);
		object.put("result", -1);
		object.put("describe", "接口参数不完整");
		object.put("code", "0024");
	    } else {
		// 查询应用信息
		AppExtendBean appExtendBean = appExtendService.queryAppExtendByIdentify(app_key);
		// 判断app信息是否为空
		if (appExtendBean != null) {
		    // 验证秘钥信息
		    if (checkValidityService.toolsCheckSign(appExtendBean.getApp_key(), appExtendBean.getKey_private(), timestamp, sign)) {
			// 判断auth_code是否正确，然后调用运营平台注册合并接口
			// 校验短信验证码
			if (smsCodeServcie.checkCode(relevance_phone, auth_code)) {
			    object = accountManageService.queryCustAccountByRelateInfo(appExtendBean.getPeriphery_code(), relevance_phone, relevance_code);
			} else {
			    // 短信验证码错误
			    object.put("result", "-1");
			    object.put("discripe", "验证码不正确");
			}
			// 获取返回结果
			String result = (String) object.get("result");
			// 判断注册是否成功
			if (result.equals("0")) {
			    object.put("result", "0");
			    object.put("describe", "账号信息列表接口查询成功");
			    object.put("code", "0000");
			}
		    } else {
			object.put("result", "-1");
			object.put("describe", "参数sign错误");
			object.put("code", "0001");
		    }
		} else {
		    object.put("result", "-1");
		    object.put("describe", "请使用正确的应用标识");
		    object.put("code", "0007");
		}
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    object.put("result", "-1");
	    object.put("describe", "错误:账号信息列表接口查询发生异常" + e);
	    object.put("code", "0100");
	    log.error("账号信息列表接口查询" + e);
	}
	// 接口调用结束时间
	double end = Calendar.getInstance().getTimeInMillis();
	// 时间差
	double during = Double.valueOf((end - begin)) / 1000;
	// 拼日志内容
	String request_msg = "app_key:" + app_key + "|timestamp:" + timestamp + "|sign:" + sign + "|ext_orgReleCode:" + relevance_code + "|ext_orgPhone:" + relevance_phone
		+ "|verifyCode:" + auth_code;
	this.logService.logInterface(during, "qryMergeableOrgList", request_msg, "数据太长,未存储", object.getString("result"), DateUtils.getCurrentTime(), this.getRequest()
		.getRemoteAddr(), null);

	String data = "";
	if (StringUtils.isNotBlank(callback)) {
	    data = callback + "(" + object.toString() + ")";
	} else {
	    data = object.toString();
	}
	return returnData(data);
    }

    /**
     * QQQQQQQQQQQQQQQQQQQQQQQQQQQ 7.1.2 账号注册接口（强制合并）
     * 客户调用此接口，只要是关联码一致的账号全部合成一个新的账号。 返回：主账号信息，副账号信息（被合并的账号）
     * 6.42　授权账号注册（强制合并T）接口
     * 
     * @return
     */
    public String orgAccRegForceJoin() {
	log.info("load orgAccRegForceJoin 账号注册接口（强制合并）");
	List<String[]> checkParamlist = new ArrayList<String[]>();
	// 计算接口调用时间-开始
	long begin = Calendar.getInstance().getTimeInMillis();
	// app标识
	String app_key = this.getRequest().getParameter("app_key");
	String[] app_keyStr = { "app_key", app_key, "50", "0", "str" };
	checkParamlist.add(app_keyStr);
	// 时间戳
	String timestamp = this.getRequest().getParameter("timestamp");
	String[] timestampStr = { "timestamp", timestamp, "20", "0", "str" };
	checkParamlist.add(timestampStr);
	// 认证码
	String sign = this.getRequest().getParameter("sign");
	String[] signStr = { "sign", sign, "50", "0", "str" };
	checkParamlist.add(signStr);
	// 企业标识
	String company_identify = this.getRequest().getParameter("ext_orgCode");
	String[] company_identifyStr = { "company_identify", company_identify, "50", "0", "str" };
	checkParamlist.add(company_identifyStr);
	// 会议状态回传地址
	String status_url = this.getRequest().getParameter("status_url");
	String[] status_urlStr = { "status_url", status_url, "200", "1", "str" };
	checkParamlist.add(status_urlStr);
	// 判断如果会议状态回传地址未传递，则设置为空字符串
	if (StringUtils.isBlank(status_url)) {
	    status_url = "";
	}
	// 话单回传地址
	String record_url = this.getRequest().getParameter("record_url");
	String[] record_urlStr = { "record_url", record_url, "200", "1", "str" };
	checkParamlist.add(record_urlStr);
	// 判断如果话单回传地址未传递，则设置为空字符串
	if (StringUtils.isBlank(record_url)) {
	    record_url = "";
	}
	// ivr结果回传地址
	String ivrdigit_url = this.getRequest().getParameter("ivrdigit_url");
	String[] ivrdigit_urlStr = { "ivrdigit_url", ivrdigit_url, "200", "1", "str" };
	checkParamlist.add(ivrdigit_urlStr);
	// 判断如果ivr结果回传地址未传递，则设置为空字符串
	if (StringUtils.isBlank(ivrdigit_url)) {
	    ivrdigit_url = "";
	}
	// 用于应用自己调用记录信息-仅用于获取~直接返回
	String callback = this.getRequest().getParameter("callback");
	// 渠道码-用于记录账号注册的渠道-用于运营平台使用
	String channel_account = this.getRequest().getParameter("channel_sysCode");
	String[] channel_sysCodeStr = { "channel_sysCode", channel_account, "20", "1", "str" };
	checkParamlist.add(channel_sysCodeStr);
	// 判断如果渠道码未传递，则设置为空字符串
	if (StringUtils.isBlank(channel_account)) {
	    channel_account = "";
	}
	// 账号关联码
	String relevance_code = this.getRequest().getParameter("ext_orgReleCode");
	String[] ext_orgReleCodeStr = { "ext_orgReleCode", relevance_code, "20", "0", "str" };
	checkParamlist.add(ext_orgReleCodeStr);
	if (StringUtils.isBlank(relevance_code)) {
	    relevance_code = "";
	}
	// 关联手机号码
	String relevance_phone = this.getRequest().getParameter("ext_orgPhone");
	String[] relevance_phoneStr = { "ext_orgPhone", relevance_phone, "20", "1", "str" };
	checkParamlist.add(relevance_phoneStr);
	if (StringUtils.isBlank(relevance_phone)) {
	    relevance_phone = "";
	}
	// 用于后面获取企业详细信息
	String authData = null;
	JSONObject object = null;
	try {
	    // 详细信息json
	    authData = new String(this.getRequest().getParameter("authData").getBytes("ISO-8859-1"), "UTF-8");
	    String[] authDataStr = { "authData", authData, "300", "0", "json" };
	    checkParamlist.add(authDataStr);
	    object = this.checkParams(checkParamlist);
	    if (null == object) {
		object = new JSONObject();
		// 查询应用信息
		AppExtendBean appExtendBean = appExtendService.queryAppExtendByIdentify(app_key);
		// 判断app信息是否为空
		if (appExtendBean != null) {
		    // 验证秘钥信息
		    if (checkValidityService.toolsCheckSign(appExtendBean.getApp_key(), appExtendBean.getKey_private(), timestamp, sign)) {
			// 转化传入的组织（个人）信息json
			JSONObject authDataJson = JSONObject.fromObject(authData);
			String companyName = authDataJson.optString("ext_orgName");
			String linker = authDataJson.optString("ext_orgLinker");
			String email = authDataJson.optString("ext_orgEmail");

			object = this.accountManageService.OauthCombineForceInterface(appExtendBean.getPeriphery_code(), company_identify, email, relevance_phone, app_key,
				channel_account, companyName, linker, relevance_code, status_url, record_url, ivrdigit_url, appExtendBean);
			// 获取返回结果
			String result = (String) object.get("result");
			// 判断注册是否成功
			if (result.equals("0")) {
			    object.put("result", "0");
			    object.put("describe", "账号注册接口（强制合并）成功");
			    object.put("code", "0000");
			} else {
			    object.put("data", "");
			}
		    } else {
			object.put("result", "-1");
			object.put("describe", "参数sign错误");
			object.put("code", "0001");
			object.put("data", "");
		    }
		} else {
		    object.put("result", "-1");
		    object.put("describe", "请使用正确的应用标识");
		    object.put("code", "0007");
		    object.put("data", "");
		}
	    } else {
		object.put("data", "");
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    object = new JSONObject();
	    object.put("result", "-1");
	    object.put("errorMsg", "错误:账号注册接口（强制合并）发生异常" + e);
	    object.put("describe", "计费账号注册时合并异常");
	    object.put("code", "0102");
	    object.put("data", "");
	    log.error("账号注册接口（强制合并）失败" + e);
	}
	// 接口调用结束时间
	double end = Calendar.getInstance().getTimeInMillis();
	// 时间差
	double during = Double.valueOf((end - begin)) / 1000;
	// 拼日志内容
	String request_msg = "app_key:" + app_key + "|timestamp:" + timestamp + "|sign:" + sign + "|ext_orgCode:" + company_identify + "|authData:" + authData + "|status_url:"
		+ status_url + "|record_url:" + record_url + "|channel_sysCode:" + channel_account + "|ext_orgReleCode:" + relevance_code + "|ext_orgPhone:" + relevance_phone;
	this.logService.logInterface(during, "orgAccRegForceJoin", request_msg, object.toString(), object.getString("result"), DateUtils.getCurrentTime(), this.getRequest()
		.getRemoteAddr(), null);

	object.remove("errorMsg"); // 数据库日志存储失败详细描述
	String data = "";
	if (StringUtils.isNotBlank(callback)) {
	    data = callback + "(" + object.toString() + ")";
	} else {
	    data = object.toString();
	}
	return returnData(data);
    }

    /**
     * QQQQQQQQQQQQQQQQQQQQQQQQQQ 7.1.3
     * 账号注册接口（普通注册，不合并）用户选择不进行合并账号，想生成一个新的账号。返回：账号注册信息 6.5　授权认证接口 运营平台接口
     * 
     * @return
     */
    public String orgAccReg() {
	log.info("load orgAccReg 账号注册");
	// 计算接口调用时间-开始
	long begin = Calendar.getInstance().getTimeInMillis();
	// 接口请求参数
	List<String[]> checkParamlist = new ArrayList<String[]>();
	// app标识
	String app_key = this.getRequest().getParameter("app_key");
	String[] app_keyStr = { "app_key", app_key, "50", "0", "str" };
	checkParamlist.add(app_keyStr);
	// 时间戳
	String timestamp = this.getRequest().getParameter("timestamp");
	String[] timestampStr = { "timestamp", timestamp, "20", "0", "str" };
	checkParamlist.add(timestampStr);
	// 认证码
	String sign = this.getRequest().getParameter("sign");
	String[] signStr = { "sign", sign, "50", "0", "str" };
	checkParamlist.add(signStr);
	// 企业标识
	String company_identify = this.getRequest().getParameter("ext_orgCode");
	String[] company_identifyStr = { "ext_orgCode", company_identify, "50", "0", "str" };
	checkParamlist.add(company_identifyStr);
	// 会议状态回传地址
	String status_url = this.getRequest().getParameter("status_url");
	String[] status_urlStr = { "status_url", status_url, "200", "1", "str" };
	checkParamlist.add(status_urlStr);
	// 判断如果会议状态回传地址未传递，则设置为空字符串
	if (StringUtils.isBlank(status_url)) {
	    status_url = "";
	}
	// 话单回传地址
	String record_url = this.getRequest().getParameter("record_url");
	String[] record_urlStr = { "record_url", record_url, "200", "1", "str" };
	checkParamlist.add(record_urlStr);
	// 判断如果话单回传地址未传递，则设置为空字符串
	if (StringUtils.isBlank(record_url)) {
	    record_url = "";
	}
	// ivr结果回传地址
	String ivrdigit_url = this.getRequest().getParameter("ivrdigit_url");
	String[] ivrdigit_urlStr = { "ivrdigit_url", ivrdigit_url, "200", "1", "str" };
	checkParamlist.add(ivrdigit_urlStr);
	// 判断如果ivr结果回传地址未传递，则设置为空字符串
	if (StringUtils.isBlank(ivrdigit_url)) {
	    ivrdigit_url = "";
	}
	// 用于应用自己调用记录信息-仅用于获取~直接返回
	String callback = this.getRequest().getParameter("callback");
	// 渠道码-用于记录账号注册的渠道-用于运营平台使用
	String channel_account = this.getRequest().getParameter("channel_sysCode");
	String[] channel_sysCodeStr = { "channel_sysCode", channel_account, "20", "1", "str" };
	checkParamlist.add(channel_sysCodeStr);
	// 判断如果渠道码未传递，则设置为空字符串
	if (StringUtils.isBlank(channel_account)) {
	    channel_account = "";
	}
	// 账号关联码
	String relevance_code = this.getRequest().getParameter("ext_orgReleCode");
	String[] ext_orgReleCodeStr = { "ext_orgReleCode", relevance_code, "20", "1", "str" };
	checkParamlist.add(ext_orgReleCodeStr);
	if (StringUtils.isBlank(relevance_code)) {
	    relevance_code = "";
	}
	// 关联手机号码
	String relevance_phone = this.getRequest().getParameter("ext_orgPhone");
	String[] relevance_phoneStr = { "ext_orgPhone", relevance_phone, "20", "1", "str" };
	checkParamlist.add(relevance_phoneStr);
	if (StringUtils.isBlank(relevance_phone)) {
	    relevance_phone = "";
	}
	// 用于后面获取企业详细信息
	String authData = null;
	JSONObject object = null;
	try {
	    // 详细信息json
	    authData = new String(this.getRequest().getParameter("authData").getBytes("ISO-8859-1"), "UTF-8");
	    String[] authDataStr = { "authData", authData, "300", "0", "json" };
	    checkParamlist.add(authDataStr);
	    object = this.checkParams(checkParamlist);
	    if (null == object) {
		object = new JSONObject();
		// 查询应用信息
		AppExtendBean appExtendBean = appExtendService.queryAppExtendByIdentify(app_key);
		// 判断app信息是否为空
		if (appExtendBean != null) {
		    // 验证秘钥信息
		    if (checkValidityService.toolsCheckSign(appExtendBean.getApp_key(), appExtendBean.getKey_private(), timestamp, sign)) {
			// 转化传入的组织（个人）信息json
			JSONObject authDataJson = JSONObject.fromObject(authData);
			String companyName = authDataJson.optString("ext_orgName");
			String linker = authDataJson.optString("ext_orgLinker");
			String email = authDataJson.optString("ext_orgEmail");
			// 账号注册调用
			object = accountManageService.accountRegiste(appExtendBean, app_key, appExtendBean.getPeriphery_code(), company_identify, relevance_code, relevance_phone,
				channel_account, companyName, linker, email, status_url, record_url, ivrdigit_url);
			// 获取返回结果
			String result = (String) object.get("result");
			// 判断注册是否成功
			if (result.equals("0")) {
			    object.put("result", "0");
			    object.put("describe", "组织账号注册成功");
			    object.put("code", "0000");
			} else {
			    object.put("data", "");
			}
		    } else {
			object.put("result", "-1");
			object.put("describe", "参数sign错误");
			object.put("code", "0001");
			object.put("data", "");
		    }
		} else {
		    object.put("result", "-1");
		    object.put("describe", "请使用正确的应用标识");
		    object.put("code", "0007");
		    object.put("data", "");
		}
	    } else {
		// 参数校验未通过
		object.put("data", "");
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    object = new JSONObject();
	    object.put("result", "-1");
	    object.put("errorMsg", "错误:组织账号注册发生异常" + e);
	    object.put("describe", "计费账号注册异常");
	    object.put("code", "0101");
	    object.put("data", "");
	    log.error("组织账号注册失败" + e);
	}
	// 接口调用结束时间
	double end = Calendar.getInstance().getTimeInMillis();
	// 时间差
	double during = Double.valueOf((end - begin)) / 1000;
	// 拼日志内容
	String request_msg = "app_key:" + app_key + "|timestamp:" + timestamp + "|sign:" + sign + "|ext_orgCode:" + company_identify + "|authData:" + authData + "|ivrdigit_url:"
		+ ivrdigit_url + "|status_url:" + status_url + "|record_url:" + record_url + "|channel_sysCode:" + channel_account + "|ext_orgReleCode:" + relevance_code
		+ "|ext_orgPhone:" + relevance_phone;
	this.logService.logInterface(during, "orgAccReg", request_msg, object.toString(), object.getString("result"), DateUtils.getCurrentTime(),
		this.getRequest().getRemoteAddr(), null);

	object.remove("errorMsg"); // 数据库日志存储失败详细描述
	String data = "";
	if (StringUtils.isNotBlank(callback)) {
	    data = callback + "(" + object.toString() + ")";
	} else {
	    data = object.toString();
	}
	return returnData(data);
    }

    /**
     * 赵希H5调用 7.1.4 账号合并接口 (获取H5调用的合并参数,然后调用运营平台合并接口 运营平台返回成功后把副账号信息与主账号信息放入线程.
     * 把所有的副账号信息对应的URL(空则不更新)都更新到主账号中 由线程向副账号对应的URL进行通知更新账号信息(主账号信息) )
     * 
     * @return
     */
    public String orgAccRegWithMerge() {
	log.info("load orgAccRegWithMerge 账号合并接口");
	// 计算接口调用时间-开始
	long begin = Calendar.getInstance().getTimeInMillis();
	// 请求参数
	// 应用标识
	String app_key = this.getRequest().getParameter("app_key");
	// cust_account账号标识
	String cust_account = this.getRequest().getParameter("cust_account");
	// 时间戳
	String timestamp = this.getRequest().getParameter("timestamp");
	// 认证码
	String sign = this.getRequest().getParameter("sign");
	// 用于应用自己调用记录信息-仅用于获取~直接返回
	String callback = this.getRequest().getParameter("callback");
	// 渠道码-用于记录账号注册的渠道-用于运营平台使用
	String channel_account = this.getRequest().getParameter("channel_sysCode");
	// 判断如果渠道码未传递，则设置为空字符串
	if (StringUtils.isBlank(channel_account)) {
	    channel_account = "";
	}
	// 是否首次注册 0 先注册后合并；1 只合并
	String is_first_oauth = this.getRequest().getParameter("is_first_oauth");
	if (StringUtils.isBlank(channel_account)) {
	    is_first_oauth = "1";
	}
	String company_identify = this.getRequest().getParameter("ext_orgCode");
	if (StringUtils.isBlank(company_identify)) {
	    company_identify = "";
	}
	// 会议状态回传地址
	String status_url = this.getRequest().getParameter("status_url");
	// 判断如果会议状态回传地址未传递，则设置为空字符串
	if (StringUtils.isBlank(status_url)) {
	    status_url = "";
	}
	// 话单回传地址
	String record_url = this.getRequest().getParameter("record_url");
	// 判断如果话单回传地址未传递，则设置为空字符串
	if (StringUtils.isBlank(record_url)) {
	    record_url = "";
	}
	// ivr结果回传地址
	String ivrdigit_url = this.getRequest().getParameter("ivrdigit_url");
	// 判断如果ivr结果回传地址未传递，则设置为空字符串
	if (StringUtils.isBlank(ivrdigit_url)) {
	    ivrdigit_url = "";
	}
	// 账号关联码
	String relevance_code = this.getRequest().getParameter("ext_orgReleCode");
	if (StringUtils.isBlank(relevance_code)) {
	    relevance_code = "";
	}
	// 关联手机号码
	String relevance_phone = this.getRequest().getParameter("ext_orgPhone");
	if (StringUtils.isBlank(relevance_phone)) {
	    relevance_phone = "";
	}
	// 被合并账号
	String accounts = this.getRequest().getParameter("accounts");
	if (StringUtils.isBlank(accounts)) {
	    accounts = "";
	}
	String authData = "";
	JSONObject object = new JSONObject();
	try {
	    authData = new String(this.getRequest().getParameter("authData").getBytes("ISO-8859-1"), "UTF-8");
	    log.info("authData------" + authData);
	    // 校验传入参数是否为空
	    if (StringUtils.isBlank(app_key) || StringUtils.isBlank(cust_account) || StringUtils.isBlank(accounts)) {
		log.info("orgAccRegWithMerge doAuth params:" + app_key + "，" + cust_account + "，" + accounts);
		object.put("result", -1);
		object.put("describe", "接口参数不完整");
		object.put("code", "0024");
	    } else {
		// 查询应用信息
		AppExtendBean appExtendBean = appExtendService.queryAppExtendByIdentify(app_key);
		// 判断app信息是否为空
		if (appExtendBean != null) {
		    // 验证秘钥信息
		    if (checkValidityService.toolsCheckSign(app_key, appExtendBean.getKey_private(), timestamp, sign)) {
			// 先注册后合并
			// 被合并账号LIST
			JSONArray accountsJSON = JSONArray.fromObject(accounts);
			if (is_first_oauth.equals("0")) {
			    // 转化传入的组织（个人）信息json
			    JSONObject authDataJson = JSONObject.fromObject(authData);
			    String companyName = authDataJson.getString("ext_orgName");
			    String linker = authDataJson.getString("ext_orgLinker");
			    String email = "";
			    if (authDataJson.containsKey("ext_orgEmail")) {
				email = authDataJson.getString("ext_orgEmail");
			    }
			    object = accountManageService.accountRegiste(appExtendBean, app_key, appExtendBean.getPeriphery_code(), company_identify, relevance_code,
				    relevance_phone, channel_account, companyName, linker, email, status_url, record_url, ivrdigit_url);
			    if (object.getString("result").equals("0")) {
				JSONObject account = object.getJSONObject("data");// 获取注册返回的对象

				JSONObject accountJSON = new JSONObject();
				accountJSON.put("cust_account", account.getString("cust_account"));// 账号
				accountJSON.put("app_key", app_key);// app_key
				accountJSON.put("is_lord_account", "0");
				accountsJSON.add(accountJSON);
			    }
			}
			// 调用注册接口失败，未合并
			if (object.containsKey("result") && object.getString("result").equals("-1")) {
			    object.put("result", "-1");
			    object.put("describe", "注册失败");
			    object.put("code", "0100");
			} else {
			    object = accountManageService.OauthCombineInterface(appExtendBean.getPeriphery_code(), app_key, company_identify, channel_account, relevance_code,
				    cust_account, accountsJSON);
			}
		    } else {
			object.put("result", "-1");
			object.put("describe", "参数sign错误");
			object.put("code", "0001");
		    }

		} else {
		    object.put("result", "-1");
		    object.put("describe", "请使用正确的应用标识");
		    object.put("code", "0007");
		}
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    object.put("result", "-1");
	    object.put("describe", "错误:账号合并接口发生异常" + e);
	    object.put("code", "0100");
	    log.error("账号合并接口失败" + e);
	}
	// 接口调用结束时间
	double end = Calendar.getInstance().getTimeInMillis();
	// 时间差
	double during = Double.valueOf((end - begin)) / 1000;
	// 拼日志内容
	String request_msg = "app_key:" + app_key + "|cust_account:" + cust_account + "|timestamp:" + timestamp + "|sign:" + sign + "|channel_sysCode:" + channel_account
		+ "|is_first_oauth:" + is_first_oauth + "|ext_orgPhone=" + relevance_phone + "|ext_orgReleCode:" + relevance_code + "|ext_orgCode" + company_identify
		+ "|accounts:" + accounts + "|authData:" + authData + "status_url:" + status_url + "|record_url:" + record_url + "|ivrdigit_url:" + ivrdigit_url;
	this.logService.logInterface(during, "orgAccRegWithMerge", request_msg, object.toString(), object.getString("result"), DateUtils.getCurrentTime(), this.getRequest()
		.getRemoteAddr(), null);

	String data = "";
	if (StringUtils.isNotBlank(callback)) {
	    data = callback + "(" + object.toString() + ")";
	} else {
	    data = object.toString();
	}
	return returnData(data);
    }

    /**
     * QQQQQQQQQQQQQQQQQQQ 7.1.5 查询账号信息接口 获取账号信息
     * 
     * @return
     * 
     */
    public String qryOrgAccInfo() {
	log.info("load qryOrgAccInfo 获取账号信息");
	long begin = Calendar.getInstance().getTimeInMillis();
	// 请求参数
	List<String[]> checkParamlist = new ArrayList<String[]>();
	// 账号标识
	String cust_account = this.getRequest().getParameter("cust_account");
	String[] cust_accountStr = { "cust_account", cust_account, "50", "0", "str" };
	checkParamlist.add(cust_accountStr);
	// 时间戳
	String timestamp = this.getRequest().getParameter("timestamp");
	String[] timestampStr = { "timestamp", timestamp, "20", "0", "str" };
	checkParamlist.add(timestampStr);
	// 认证码
	String sign = this.getRequest().getParameter("sign");
	String[] signStr = { "sign", sign, "50", "0", "str" };
	checkParamlist.add(signStr);
	// 此参数用于其他应用中间参数传递，不做任何处理
	String callback = this.getRequest().getParameter("callback");
	// 最终返回值
	JSONObject object = null;
	try {
	    object = this.checkParams(checkParamlist);
	    if (null == object) {
		object = new JSONObject();

		// 根据cust_account查询账号信息
		AccountBean accountBean = accountExtendService.queryAccountDetailByCustAccount(cust_account);
		// 判断账号信息是否找到
		if (accountBean != null) {
		    // 验证秘钥信息
		    if (checkValidityService.toolsCheckSign(cust_account, accountBean.getKey_private(), timestamp, sign)) {
			JSONObject dataObject = new JSONObject();
			// 获取账号相关信息
			JSONObject cust = accountManageService.queryCustomerInfo(cust_account);
			if (cust != null) {
			    dataObject.put("ext_orgName", URLDecoder.decode(cust.getString("cust_name"), "UTF-8"));
			} else {
			    dataObject.put("ext_orgName", "");
			}
			dataObject.put("cust_account", cust_account);
			dataObject.put("org_tempKey", accountBean.getKey_private());
			dataObject.put("org_permKey", accountBean.getKey_public());
			dataObject.put("ext_orgLinker", accountBean.getLinker_name());
			dataObject.put("ext_orgPhone", accountBean.getLinker_phone());
			dataObject.put("ext_orgEmail", accountBean.getLinker_email());
			dataObject.put("url_callstatus", accountBean.getUrl_callstatus());
			dataObject.put("url_ivrdigit", accountBean.getUrl_ivrdigit());
			dataObject.put("url_ticket", accountBean.getUrl_ticket());

			// 拼写最终返回json
			object.put("result", "0");
			object.put("code", "0000");
			object.put("describe", "success");
			object.put("data", dataObject);
		    } else {
			object.put("result", "-1");
			object.put("describe", "参数sign错误");
			object.put("code", "0001");
			object.put("data", "");
		    }
		} else {
		    object.put("result", "-1");
		    object.put("describe", "计费账号不存在");
		    object.put("code", "0002");
		    object.put("data", "");
		}
	    } else {
		// 参数检验未通过
		object.put("data", "");
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    object = new JSONObject();
	    object.put("result", "-1");
	    object.put("errorMsg", "错误:查询账号信息接口异常" + e);
	    object.put("describe", "计费账号查询异常");
	    object.put("code", "0103");
	    object.put("data", "");
	    log.error("查询账号信息接口失败" + e);
	}
	// 写日志
	double end = Calendar.getInstance().getTimeInMillis();
	double during = Double.valueOf((end - begin)) / 1000;
	String request_msg = "cust_account:" + cust_account + "|timestamp:" + timestamp + "|sign:" + sign;
	this.logService.logInterface(during, "qryOrgAccInfo", request_msg, object.toString(), object.getString("result"), DateUtils.getCurrentTime(), this.getRequest()
		.getRemoteAddr(), null);

	object.remove("errorMsg"); // 数据库日志存储失败详细描述
	String data = "";
	if (StringUtils.isNotBlank(callback)) {
	    data = callback + "(" + object.toString() + ")";
	} else {
	    data = object.toString();
	}

	return returnData(data);
    }

    /**
     * QQQQQQQQQQQQQQQQQQQQQQQQ 7.1.6 账号信息更新接口（强制合并） 更新账号信息(强制合并)
     * 
     * @return
     */
    public String updOrgAccInfoForceJoin() {
	log.info("load updOrgAccInfoForceJoin 账号信息更新接口（强制合并）");
	// 计算接口调用时间-开始
	long begin = Calendar.getInstance().getTimeInMillis();
	// 请求参数
	List<String[]> checkParamlist = new ArrayList<String[]>();
	// 应用标识
	String app_key = this.getRequest().getParameter("app_key");
	String[] app_keyStr = { "app_key", app_key, "50", "0", "str" };
	checkParamlist.add(app_keyStr);
	// cust_account账号标识
	String cust_account = this.getRequest().getParameter("cust_account");
	String[] cust_accountStr = { "cust_account", cust_account, "50", "0", "str" };
	checkParamlist.add(cust_accountStr);
	// 时间戳
	String timestamp = this.getRequest().getParameter("timestamp");
	String[] timestampStr = { "timestamp", timestamp, "20", "0", "str" };
	checkParamlist.add(timestampStr);
	// 认证码
	String sign = this.getRequest().getParameter("sign");
	String[] signStr = { "sign", sign, "50", "0", "str" };
	checkParamlist.add(signStr);

	// 用于应用自己调用记录信息-仅用于获取~直接返回
	String callback = this.getRequest().getParameter("callback");
	// 渠道码-用于记录账号注册的渠道-用于运营平台使用
	String channel_account = this.getRequest().getParameter("channel_sysCode");
	String[] channel_sysCodeStr = { "channel_sysCode", channel_account, "20", "1", "str" };
	checkParamlist.add(channel_sysCodeStr);
	// 判断如果渠道码未传递，则设置为空字符串
	if (StringUtils.isBlank(channel_account)) {
	    channel_account = "";
	}
	// 账号关联码
	String relevance_code = this.getRequest().getParameter("ext_orgReleCode");
	String[] ext_orgReleCodeStr = { "ext_orgReleCode", relevance_code, "20", "0", "str" };
	checkParamlist.add(ext_orgReleCodeStr);
	if (StringUtils.isBlank(relevance_code)) {
	    relevance_code = "";
	}
	// 关联手机号码
	String relevance_phone = this.getRequest().getParameter("ext_orgPhone");
	String[] relevance_phoneStr = { "ext_orgPhone", relevance_phone, "20", "0", "str" };
	checkParamlist.add(relevance_phoneStr);
	if (StringUtils.isBlank(relevance_phone)) {
	    relevance_phone = "";
	}
	// 会议状态回传地址
	String status_url = this.getRequest().getParameter("status_url");
	String[] status_urlStr = { "status_url", status_url, "200", "1", "str" };
	checkParamlist.add(status_urlStr);
	// 判断如果会议状态回传地址未传递，则设置为空字符串
	if (StringUtils.isBlank(status_url)) {
	    status_url = "";
	}
	// 话单回传地址
	String record_url = this.getRequest().getParameter("record_url");
	String[] record_urlStr = { "record_url", record_url, "200", "1", "str" };
	checkParamlist.add(record_urlStr);
	// 判断如果话单回传地址未传递，则设置为空字符串
	if (StringUtils.isBlank(record_url)) {
	    record_url = "";
	}
	// ivr结果回传地址
	String ivrdigit_url = this.getRequest().getParameter("ivrdigit_url");
	String[] ivrdigit_urlStr = { "ivrdigit_url", ivrdigit_url, "200", "1", "str" };
	checkParamlist.add(ivrdigit_urlStr);
	// 判断如果ivr结果回传地址未传递，则设置为空字符串
	if (StringUtils.isBlank(ivrdigit_url)) {
	    ivrdigit_url = "";
	}
	// 用于后面获取企业详细信息
	String authData = null;
	JSONObject object = null;
	try {
	    // 详细信息json
	    authData = new String(this.getRequest().getParameter("authData").getBytes("ISO-8859-1"), "UTF-8");
	    String[] authDataStr = { "authData", authData, "300", "0", "json" };
	    checkParamlist.add(authDataStr);
	    object = this.checkParams(checkParamlist);
	    if (null == object) {
		object = new JSONObject();
		// 转化传入的组织（个人）信息json
		JSONObject authDataJson = JSONObject.fromObject(authData);
		String companyName = authDataJson.optString("ext_orgName");
		String linker = authDataJson.optString("ext_orgLinker");
		String email = authDataJson.optString("ext_orgEmail");
		// 查询应用信息
		AppExtendBean appExtendBean = appExtendService.queryAppExtendByIdentify(app_key);
		// 判断app信息是否为空
		if (appExtendBean != null) {
		    // 根据账号cust_account查询账号信息
		    AccountBean accountBean = accountExtendService.queryAccountDetailByCustAccount(cust_account);
		    // 根据查询的账号信息
		    // 判断账号信息是否找到
		    if (accountBean != null) {
			// 验证秘钥信息
			if (checkValidityService.toolsCheckSign(cust_account, accountBean.getKey_private(), timestamp, sign)) {
			    if (relevance_code.length() > 0) {
				// 调用运营平台注册更新合并接口
				object = accountManageService.UpdateCustomerForceInfo(app_key, appExtendBean.getPeriphery_code(), cust_account, email, relevance_phone,
					channel_account, companyName, linker, relevance_code, status_url, record_url, ivrdigit_url);
			    } else {
				object.put("result", "-1");
				object.put("describe", "账号关联码为空");
			    }

			    // 获取返回结果
			    String result = (String) object.get("result");
			    // 判断注册是否成功
			    if (result.equals("0")) {
				object.put("result", "0");
				object.put("describe", "账号信息更新成功");
				object.put("code", "0000");
			    }
			} else {
			    object.put("result", "-1");
			    object.put("describe", "参数sign错误");
			    object.put("code", "0001");
			}
		    } else {
			object.put("result", "-1");
			object.put("describe", "计费账号不存在");
			object.put("code", "0002");
		    }
		} else {
		    object.put("result", "-1");
		    object.put("describe", "请使用正确的应用标识");
		    object.put("code", "0007");
		}
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    object = new JSONObject();
	    object.put("result", "-1");
	    object.put("describe", "错误:账号信息更新接口（强制合并）发生异常" + e);
	    object.put("code", "0100");
	    log.error("账号信息更新接口（强制合并）失败" + e);
	}
	// 接口调用结束时间
	double end = Calendar.getInstance().getTimeInMillis();
	// 时间差
	double during = Double.valueOf((end - begin)) / 1000;
	// 拼日志内容
	String request_msg = "app_key:" + app_key + "|cust_account:" + cust_account + "|timestamp:" + timestamp + "|sign:" + sign + "|authData:" + authData + "|channel_sysCode:"
		+ channel_account + "|ext_orgReleCode" + relevance_code + "|ext_orgPhone" + relevance_phone + "|status_url:" + status_url + "|record_url:" + record_url
		+ "|ivrdigit_url:" + ivrdigit_url;

	this.logService.logInterface(during, "updOrgAccInfoForceJoin", request_msg, object.toString(), object.getString("result"), DateUtils.getCurrentTime(), this.getRequest()
		.getRemoteAddr(), null);

	String data = "";
	if (StringUtils.isNotBlank(callback)) {
	    data = callback + "(" + object.toString() + ")";
	} else {
	    data = object.toString();
	}
	return returnData(data);
    }

    /**
     * QQQQQQQQQQQQQQQQQQQQQ 7.1.7 账号信息更新接口（普通更新，不合并） 更新账号信息
     * 
     * @return
     */
    public String updOrgAccInfo() {
	log.info("load updOrgAccInfo 账号信息更新");
	// 计算接口调用时间-开始
	long begin = Calendar.getInstance().getTimeInMillis();
	// 请求参数
	List<String[]> checkParamlist = new ArrayList<String[]>();
	// 应用标识
	String app_key = this.getRequest().getParameter("app_key");
	String[] app_keyStr = { "app_key", app_key, "50", "0", "str" };
	checkParamlist.add(app_keyStr);
	// cust_account账号标识
	String cust_account = this.getRequest().getParameter("cust_account");
	String[] cust_accountStr = { "cust_account", cust_account, "50", "0", "str" };
	checkParamlist.add(cust_accountStr);
	// 时间戳
	String timestamp = this.getRequest().getParameter("timestamp");
	String[] timestampStr = { "timestamp", timestamp, "20", "0", "str" };
	checkParamlist.add(timestampStr);
	// 认证码
	String sign = this.getRequest().getParameter("sign");
	String[] signStr = { "sign", sign, "50", "0", "str" };
	checkParamlist.add(signStr);
	// 会议状态回传地址
	String status_url = this.getRequest().getParameter("status_url");
	String[] status_urlStr = { "status_url", status_url, "200", "1", "str" };
	checkParamlist.add(status_urlStr);
	// 判断如果会议状态回传地址未传递，则设置为空字符串
	if (StringUtils.isBlank(status_url)) {
	    status_url = "";
	}
	// 话单回传地址
	String record_url = this.getRequest().getParameter("record_url");
	String[] record_urlStr = { "record_url", record_url, "200", "1", "str" };
	checkParamlist.add(record_urlStr);
	// 判断如果话单回传地址未传递，则设置为空字符串
	if (StringUtils.isBlank(record_url)) {
	    record_url = "";
	}
	// ivr结果回传地址
	String ivrdigit_url = this.getRequest().getParameter("ivrdigit_url");
	String[] ivrdigit_urlStr = { "ivrdigit_url", ivrdigit_url, "200", "1", "str" };
	checkParamlist.add(ivrdigit_urlStr);
	// 判断如果ivr结果回传地址未传递，则设置为空字符串
	if (StringUtils.isBlank(ivrdigit_url)) {
	    ivrdigit_url = "";
	}
	// 用于应用自己调用记录信息-仅用于获取~直接返回
	String callback = this.getRequest().getParameter("callback");
	// 渠道码-用于记录账号注册的渠道-用于运营平台使用
	String channel_account = this.getRequest().getParameter("channel_sysCode");
	String[] channel_sysCodeStr = { "channel_sysCode", channel_account, "20", "1", "str" };
	checkParamlist.add(channel_sysCodeStr);
	// 判断如果渠道码未传递，则设置为空字符串
	if (StringUtils.isBlank(channel_account)) {
	    channel_account = "";
	}
	// 账号关联码
	String relevance_code = this.getRequest().getParameter("ext_orgReleCode");
	String[] ext_orgReleCodeStr = { "ext_orgReleCode", relevance_code, "20", "1", "str" };
	checkParamlist.add(ext_orgReleCodeStr);
	if (StringUtils.isBlank(relevance_code)) {
	    relevance_code = "";
	}
	// 关联手机号码
	String relevance_phone = this.getRequest().getParameter("ext_orgPhone");
	String[] relevance_phoneStr = { "ext_orgPhone", relevance_phone, "20", "1", "str" };
	checkParamlist.add(relevance_phoneStr);
	if (StringUtils.isBlank(relevance_phone)) {
	    relevance_phone = "";
	}
	// 用于后面获取企业详细信息
	String authData = null;
	JSONObject object = new JSONObject();
	try {
	    // 详细信息json
	    authData = new String(this.getRequest().getParameter("authData").getBytes("ISO-8859-1"), "UTF-8");
	    String[] authDataStr = { "authData", authData, "300", "0", "json" };
	    checkParamlist.add(authDataStr);
	    object = this.checkParams(checkParamlist);
	    if (null == object) {
		object = new JSONObject();
		// 转化传入的组织（个人）信息json
		JSONObject authDataJson = JSONObject.fromObject(authData);
		String companyName = authDataJson.optString("ext_orgName");
		String linker = authDataJson.optString("ext_orgLinker");
		String email = authDataJson.optString("ext_orgEmail");
		// 查询应用信息
		AppExtendBean appExtendBean = appExtendService.queryAppExtendByIdentify(app_key);
		// 判断app信息是否为空
		if (appExtendBean != null) {
		    // 根据账号cust_account查询账号信息
		    AccountBean accountBean = accountExtendService.queryAccountDetailByCustAccount(cust_account);
		    // 根据查询的账号信息
		    // 判断账号信息是否找到
		    if (accountBean != null) {
			// 验证秘钥信息
			if (checkValidityService.toolsCheckSign(cust_account, accountBean.getKey_private(), timestamp, sign)) {

			    object = accountManageService.UpdateCustomerInfo(cust_account, email, relevance_phone, channel_account, companyName, linker, relevance_code,
				    status_url, record_url, ivrdigit_url);
			    // 获取返回结果
			    String result = (String) object.get("result");
			    // 判断注册是否成功
			    if (result.equals("0")) {
				object.put("result", "0");
				object.put("describe", "账号信息更新成功");
				object.put("code", "0000");
			    } else {
				object.put("data", "");
			    }
			} else {
			    object.put("result", "-1");
			    object.put("describe", "参数sign错误");
			    object.put("code", "0001");
			    object.put("data", "");
			}
		    } else {
			object.put("result", "-1");
			object.put("describe", "计费账号不存在");
			object.put("code", "0002");
			object.put("data", "");
		    }
		} else {
		    object.put("result", "-1");
		    object.put("describe", "请使用正确的应用标识");
		    object.put("code", "0007");
		    object.put("data", "");
		}
	    } else {
		// 参数校验未通过
		object.put("data", "");
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    object = new JSONObject();
	    object.put("result", "-1");
	    object.put("errorMsg", "错误:组织账号更新发生异常" + e);
	    object.put("describe", "计费账号更新异常");
	    object.put("code", "0105");
	    object.put("data", "");
	    log.error("组织账号更新失败" + e);
	}
	// 接口调用结束时间
	double end = Calendar.getInstance().getTimeInMillis();
	// 时间差
	double during = Double.valueOf((end - begin)) / 1000;
	// 拼日志内容
	String request_msg = "app_key:" + app_key + "|cust_account:" + cust_account + "|timestamp:" + timestamp + "|sign:" + sign + "|authData:" + authData + "|ivrdigit_url:"
		+ ivrdigit_url + "|status_url:" + status_url + "|record_url:" + record_url + "|channel_sysCode:" + channel_account + "|ext_orgReleCode:" + relevance_code
		+ "|ext_orgPhone:" + relevance_phone;
	this.logService.logInterface(during, "updOrgAccInfo", request_msg, object.toString(), object.getString("result"), DateUtils.getCurrentTime(), this.getRequest()
		.getRemoteAddr(), null);

	object.remove("errorMsg"); // 数据库日志存储失败详细描述
	String data = "";
	if (StringUtils.isNotBlank(callback)) {
	    data = callback + "(" + object.toString() + ")";
	} else {
	    data = object.toString();
	}
	return returnData(data);
    }

    /**
     * QQQQQQQQQQQQQQQQQQQQ 7.1.8 账号的临时密钥获取（更新）接口 获取账号秘钥
     * 
     * @return
     */
    public String updOrgAccTempKey() {
	log.info("load updOrgAccTempKey");
	long begin = Calendar.getInstance().getTimeInMillis();
	// 请求参数
	List<String[]> checkParamlist = new ArrayList<String[]>();
	// 账号标识
	String cust_Account = this.getRequest().getParameter("cust_account");
	String[] cust_accountStr = { "cust_account", cust_Account, "50", "0", "str" };
	checkParamlist.add(cust_accountStr);
	// 时间戳
	String timestamp = this.getRequest().getParameter("timestamp");
	String[] timestampStr = { "timestamp", timestamp, "20", "0", "str" };
	checkParamlist.add(timestampStr);
	// 认证码
	String sign = this.getRequest().getParameter("sign");
	String[] signStr = { "sign", sign, "50", "0", "str" };
	checkParamlist.add(signStr);
	// jsonp处理
	String callback = this.getRequest().getParameter("callback");

	JSONObject object = null;
	try {
	    object = this.checkParams(checkParamlist);
	    if (null == object) {
		object = new JSONObject();
		// 获取临时秘钥并更新账号临时秘钥
		object = accountExtendService.updateAccountkey(cust_Account, sign, timestamp);
	    } else {
		object.put("key_temp", "");
	    }
	} catch (Exception ex) {
	    log.error("********获取临时密钥失败**********"+ex);
	    object = new JSONObject();
	    object.put("key_temp", "");
	    object.put("code", "0104");
	    object.put("result", "-1");
	    object.put("describe", "更新临时密钥异常");
	    object.put("errorMsg", "错误:" + ex.getMessage());
	}

	// 接口调用结束时间
	double end = Calendar.getInstance().getTimeInMillis();
	// 时间差
	double during = Double.valueOf((end - begin)) / 1000;
	// 拼日志内容
	String request_msg = "cust_Account:" + cust_Account + "|timestamp:" + timestamp + "|sign:" + sign;
	this.logService.logInterface(during, "updOrgAccTempKey", request_msg, object.toString(), object.getString("result"), DateUtils.getCurrentTime(), this.getRequest()
		.getRemoteAddr(), null);

	object.remove("errorMsg"); // 数据库日志存储失败详细描述
	String data = "";
	if (StringUtils.isNotBlank(callback)) {
	    data = callback + "(" + object.toString() + ")";
	} else {
	    data = object.toString();
	}

	return returnData(data);
    }

    /**
     * 赵希H5调用 8.1 更新组织账号关联码（返回账号列表） 更新关联码,并返回账号列表
     * 
     * @return
     */
    public String updOrgAccByReleCode() {
	log.info("load updOrgAccByReleCode 更新组织账号关联码（返回账号列表）");
	// 计算接口调用时间-开始
	long begin = Calendar.getInstance().getTimeInMillis();
	// 请求参数
	// app标识
	String app_key = this.getRequest().getParameter("app_key");
	// cust_account
	String cust_account = this.getRequest().getParameter("cust_account");
	// 时间戳
	String timestamp = this.getRequest().getParameter("timestamp");
	// 认证码
	String sign = this.getRequest().getParameter("sign");

	// 用于应用自己调用记录信息-仅用于获取~直接返回
	String callback = this.getRequest().getParameter("callback");

	// 账号关联码
	String relevance_code = this.getRequest().getParameter("ext_orgReleCode");
	if (StringUtils.isBlank(relevance_code)) {
	    relevance_code = "";
	}
	// 渠道码-用于记录账号注册的渠道-用于运营平台使用
	String channel_account = this.getRequest().getParameter("channel_sysCode");
	if (StringUtils.isBlank(channel_account)) {
	    channel_account = "";
	}
	// 关联手机号码
	String relevance_phone = this.getRequest().getParameter("ext_orgPhone");
	if (StringUtils.isBlank(relevance_phone)) {
	    relevance_phone = "";
	}
	String email = this.getRequest().getParameter("ext_orgEmail");
	if (StringUtils.isBlank(email)) {
	    email = "";
	}
	// 验证码
	String auth_code = this.getRequest().getParameter("verifyCode");
	if (StringUtils.isBlank(auth_code)) {
	    auth_code = "";
	}

	String h5Demo_code = this.getRequest().getParameter("h5Demo_code");
	if (StringUtils.isBlank(h5Demo_code)) {
	    h5Demo_code = "";
	}
	// 用于后面获取企业详细信息
	JSONObject object = new JSONObject();
	try {
	    // 校验传入参数是否为空
	    if (StringUtils.isBlank(app_key) || StringUtils.isBlank(relevance_code) || StringUtils.isBlank(relevance_phone)) {
		log.info("updOrgAccByReleCode doAuth params:" + app_key + "，" + relevance_code + "，" + relevance_phone);
		object.put("result", -1);
		object.put("describe", "接口参数不完整");
		object.put("code", "0024");
	    } else {
		// 查询应用信息
		AppExtendBean appExtendBean = appExtendService.queryAppExtendByIdentify(app_key);
		// 判断app信息是否为空
		if (appExtendBean != null) {
		    AccountBean accountBean = accountExtendService.queryAccountDetailByCustAccount(cust_account);
		    // 根据查询的账号信息
		    // 判断账号信息是否找到
		    if (accountBean != null) {
			// 验证秘钥信息
			if (checkValidityService.toolsCheckSign(cust_account, accountBean.getKey_private(), timestamp, sign)) {
			    // 判断auth_code是否正确，然后调用运营平台注册合并接口
			    if (EnumUtility.isNotBlank(h5Demo_code) && h5Demo_code.equals("dudu")) {
				object = accountManageService.updQueryCustAccountByRelateInfo(cust_account, app_key, relevance_phone, channel_account, relevance_code,
					appExtendBean.getPeriphery_code(), email);
			    } else {
				// 校验短信验证码
				if (smsCodeServcie.checkCode(relevance_phone, auth_code)) {
				    object = accountManageService.updQueryCustAccountByRelateInfo(cust_account, app_key, relevance_phone, channel_account, relevance_code,
					    appExtendBean.getPeriphery_code(), email);
				} else {
				    // 短信验证码错误
				    object.put("result", "-1");
				    object.put("discripe", "验证码不正确");
				}
			    }

			    // 获取返回结果
			    String result = (String) object.get("result");
			    // 判断注册是否成功
			    if (result.equals("0")) {
				object.put("result", "0");
				object.put("describe", "更新组织账号关联码（返回账号列表）成功");
				object.put("code", "0000");
			    }
			} else {
			    object.put("result", "-1");
			    object.put("describe", "参数sign错误");
			    object.put("code", "0001");
			}
		    } else {
			object.put("result", "-1");
			object.put("describe", "未找到相应账号信息！");
			object.put("code", "0100");
		    }
		} else {
		    object.put("result", "-1");
		    object.put("describe", "请使用正确的应用标识");
		    object.put("code", "0007");
		}
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    object.put("result", "-1");
	    object.put("describe", "错误:更新组织账号关联码（返回账号列表）发生异常" + e);
	    object.put("code", "0100");
	    log.error("账号信息列表接口查询" + e);
	}
	// 接口调用结束时间
	double end = Calendar.getInstance().getTimeInMillis();
	// 时间差
	double during = Double.valueOf((end - begin)) / 1000;
	// 拼日志内容
	String request_msg = "app_key:" + app_key + "|cust_account:" + cust_account + "|timestamp:" + timestamp + "|sign:" + sign + "|ext_orgReleCode:" + relevance_code
		+ "|ext_orgPhone:" + relevance_phone + "|channel_sysCode:" + channel_account + "|ext_orgEmail:" + email + "|verifyCode:" + auth_code + "|h5Demo_code:"
		+ h5Demo_code;
	this.logService.logInterface(during, "updOrgAccByReleCode", request_msg, "数据太长,未存储", object.getString("result"), DateUtils.getCurrentTime(), this.getRequest()
		.getRemoteAddr(), null);

	String data = "";
	if (StringUtils.isNotBlank(callback)) {
	    data = callback + "(" + object.toString() + ")";
	} else {
	    data = object.toString();
	}
	return returnData(data);
    }

    /**
     * 赵希H5调用 8.4
     * 组织账号合并----------------------------------------------------------- 7.2.0
     * 合并计费账户接口
     * 
     * @param app_key
     * @param cust_account
     * @param accounts
     * @return
     */
    public String orgAccMerge() {
	log.info("load orgAccMerge 合并计费账户接口");
	// 计算接口调用时间-开始
	long begin = Calendar.getInstance().getTimeInMillis();
	// 请求参数
	// 应用标识
	String app_key = this.getRequest().getParameter("app_key");
	// cust_account账号标识
	String cust_account = this.getRequest().getParameter("cust_account");
	String cust_account_old = this.getRequest().getParameter("cust_account_old");
	// 时间戳
	String timestamp = this.getRequest().getParameter("timestamp");
	// 认证码
	String sign = this.getRequest().getParameter("sign");
	// 用于应用自己调用记录信息-仅用于获取~直接返回
	String callback = this.getRequest().getParameter("callback");
	// 渠道码-用于记录账号注册的渠道-用于运营平台使用
	String channel_account = this.getRequest().getParameter("channel_sysCode");
	// 判断如果渠道码未传递，则设置为空字符串
	if (StringUtils.isBlank(channel_account)) {
	    channel_account = "";
	}
	// 被合并账号
	String accounts = this.getRequest().getParameter("accounts");
	if (StringUtils.isBlank(accounts)) {
	    accounts = "";
	}
	JSONObject object = new JSONObject();
	try {
	    // 校验传入参数是否为空
	    if (StringUtils.isBlank(app_key) || StringUtils.isBlank(cust_account) || StringUtils.isBlank(accounts)) {
		log.info("orgAccMerge doAuth params:" + app_key + "，" + cust_account + "，" + accounts);
		object.put("result", -1);
		object.put("describe", "接口参数不完整");
		object.put("code", "0024");
	    } else {
		// 查询应用信息
		AppExtendBean appExtendBean = appExtendService.queryAppExtendByIdentify(app_key);
		// 判断app信息是否为空
		if (appExtendBean != null) {
		    // 根据账号cust_account查询账号信息
		    AccountBean accountBean = accountExtendService.queryAccountDetailByCustAccount(cust_account_old);
		    // 根据查询的账号信息
		    // 判断账号信息是否找到
		    if (accountBean != null) {
			// 验证秘钥信息
			if (checkValidityService.toolsCheckSign(cust_account_old, accountBean.getKey_private(), timestamp, sign)) {
			    // 先注册后合并
			    // 被合并账号LIST

			    JSONArray accountsJSON = JSONArray.fromObject(accounts);
			    object = accountManageService.CombineCustAccount(app_key, cust_account, accountsJSON);
			} else {
			    object.put("result", "-1");
			    object.put("describe", "参数sign错误");
			    object.put("code", "0001");
			}
		    } else {
			object.put("result", "-1");
			object.put("describe", "未找到相应账号信息！");
			object.put("code", "0100");
		    }

		} else {
		    object.put("result", "-1");
		    object.put("describe", "请使用正确的应用标识");
		    object.put("code", "0007");
		}
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    object.put("result", "-1");
	    object.put("describe", "错误:账号合并接口发生异常" + e);
	    object.put("code", "0100");
	    log.error("账号合并接口失败" + e);
	}
	// 接口调用结束时间
	double end = Calendar.getInstance().getTimeInMillis();
	// 时间差
	double during = Double.valueOf((end - begin)) / 1000;
	// 拼日志内容
	String request_msg = "app_key:" + app_key + "|cust_account:" + cust_account + "|cust_account_old:" + cust_account_old + "|timestamp:" + timestamp + "|sign:" + sign
		+ "|channel_sysCode:" + channel_account + "|accounts:" + accounts;
	this.logService.logInterface(during, "orgAccMerge", request_msg, object.toString(), object.getString("result"), DateUtils.getCurrentTime(), this.getRequest()
		.getRemoteAddr(), null);

	String data = "";
	if (StringUtils.isNotBlank(callback)) {
	    data = callback + "(" + object.toString() + ")";
	} else {
	    data = object.toString();
	}
	return returnData(data);
    }

    /**
     * QQQQQQQQQQQQQQQQQQQQ 组织账号找回接口
     * 
     * @return
     */
    @Override
    public String qryOrgAccByAppkey() {
	log.info("load qryOrgAccInfo 获取账号信息");
	long begin = Calendar.getInstance().getTimeInMillis();
	// 请求参数
	List<String[]> checkParamlist = new ArrayList<String[]>();
	// 应用标识
	String app_key = this.getRequest().getParameter("app_key");
	String[] app_keyStr = { "app_key", app_key, "50", "0", "str" };
	checkParamlist.add(app_keyStr);
	// 时间戳
	String timestamp = this.getRequest().getParameter("timestamp");
	String[] timestampStr = { "timestamp", timestamp, "20", "0", "str" };
	checkParamlist.add(timestampStr);
	// 认证码
	String sign = this.getRequest().getParameter("sign");
	String[] signStr = { "sign", sign, "50", "0", "str" };
	checkParamlist.add(signStr);
	// 企业标识
	String company_identify = this.getRequest().getParameter("ext_orgCode");
	String[] company_identifyStr = { "ext_orgCode", company_identify, "50", "0", "str" };
	checkParamlist.add(company_identifyStr);
	// 此参数用于其他应用中间参数传递，不做任何处理
	String callback = this.getRequest().getParameter("callback");
	// 最终返回值
	JSONObject object = new JSONObject();
	try {
	    object = this.checkParams(checkParamlist);
	    if (null == object) {
		object = new JSONObject();
		AppExtendBean appExtendBean = appExtendService.queryAppExtendByIdentify(app_key);
		if (appExtendBean != null) {
		    if (checkValidityService.toolsCheckSign(app_key, appExtendBean.getKey_private(), timestamp, sign)) {
			// 从运营平台获取custAccount
			String cust_account = custAccountService.getCustInfo(company_identify, app_key);
			if (StringUtils.isNotBlank(cust_account)) {
			    // 根据cust_account查询账号信息
			    AccountBean accountBean = accountExtendService.queryAccountDetailByCustAccount(cust_account);
			    // 判断账号信息是否找到
			    if (accountBean != null) {
				JSONObject dataObject = new JSONObject();
				// 获取账号相关信息
				JSONObject cust = accountManageService.queryCustomerInfo(cust_account);
				if (cust != null) {
				    dataObject.put("ext_orgName", URLDecoder.decode(cust.optString("cust_name"), "UTF-8"));
				} else {
				    dataObject.put("ext_orgName", "");
				}
				dataObject.put("cust_account", cust_account);
				dataObject.put("org_tempKey", accountBean.getKey_private());
				dataObject.put("org_permKey", accountBean.getKey_public());
				dataObject.put("ext_orgLinker", accountBean.getLinker_name());
				dataObject.put("ext_orgPhone", accountBean.getLinker_phone());
				dataObject.put("ext_orgEmail", accountBean.getLinker_email());
				dataObject.put("url_callstatus", accountBean.getUrl_callstatus());
				dataObject.put("url_ivrdigit", accountBean.getUrl_ivrdigit());
				dataObject.put("url_ticket", accountBean.getUrl_ticket());

				// 拼写最终返回json
				object.put("result", "0");
				object.put("code", "0000");
				object.put("describe", "success");
				object.put("data", dataObject);
			    } else {
				object.put("result", "-1");
				object.put("describe", "计费账号不存在");
				object.put("code", "0002");
				object.put("data", "");
			    }
			} else {
			    object.put("result", "-1");
			    object.put("describe", "计费账号不存在");
			    object.put("code", "0002");
			    object.put("data", "");
			}

		    } else {
			object.put("result", "-1");
			object.put("describe", "参数sign错误");
			object.put("code", "0001");
			object.put("data", "");
		    }
		} else {
		    object.put("result", "-1");
		    object.put("describe", "请使用正确的应用标识");
		    object.put("code", "0007");
		    object.put("data", "");
		}

	    } else {
		// 参数检验未通过
		object.put("data", "");
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    object = new JSONObject();
	    object.put("result", "-1");
	    object.put("errorMsg", "错误" + e.getMessage());
	    object.put("code", "0103");
	    object.put("describe", "计费账号查询异常");
	    object.put("data", "");
	    log.error("计费账号查询异常:"+e);
	}

	// 写日志
	double end = Calendar.getInstance().getTimeInMillis();
	double during = Double.valueOf((end - begin)) / 1000;
	String request_msg = "app_key:" + app_key + "|timestamp:" + timestamp + "|sign:" + sign + "|ext_orgCode:" + company_identify;

	this.logService.logInterface(during, "qryOrgAccByAppKey", request_msg, "数据太长，不处理", object.getString("result"), DateUtils.getCurrentTime(), this.getRequest()
		.getRemoteAddr(), null);

	object.remove("errorMsg"); // 数据库日志存储失败详细描述
	String data = "";
	if (StringUtils.isNotBlank(callback)) {
	    data = callback + "(" + object.toString() + ")";
	} else {
	    data = object.toString();
	}

	return returnData(data);
    }

}
